package com.xudow.app.ui.adapter.user;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.activeshare.edu.ucenter.models.base.AgencySchools;
import com.activeshare.edu.ucenter.models.base.Courses;
import com.activeshare.edu.ucenter.models.course.CourseWithOtherInfo;
import com.activeshare.edu.ucenter.models.user.UserFavoriteItemsWithOther;
import com.daimajia.swipe.SwipeLayout;
import com.xudow.app.Config;
import com.xudow.app.R;
import com.xudow.app.XApplication;
import com.xudow.app.newui.bean.FavoriteListMessageLS;
import com.xudow.app.util.CourseTypeUtils;
import com.xudow.app.util.ImageUtils;
import com.xudow.app.util.LngLatUtil;
import com.xudow.app.util.StringUtils;

import java.util.List;

import static com.xudow.app.util.LogUtils.makeLogTag;

/**
 * 我的收藏(课程) List Adapter
 *
 * @author lukecheng
 */
public class FavoriteCourseAdapter extends RecyclerView.Adapter {
    private static final String TAG = makeLogTag(FavoriteCourseAdapter.class);

    public interface OnRecyclerViewListener {
        void onItemClick(int position);

        boolean onItemLongClick(int position);

        void onDelete(int position);
    }

    private OnRecyclerViewListener onRecyclerViewListener;

    public void setOnRecyclerViewListener(OnRecyclerViewListener onRecyclerViewListener) {
        this.onRecyclerViewListener = onRecyclerViewListener;
    }

    /**
     * 收藏
     */
    private List<FavoriteListMessageLS.FavoriteListEntity> favorites;
    private Context context;

    public FavoriteCourseAdapter(Context context, List<FavoriteListMessageLS.FavoriteListEntity> favorites) {
        this.context = context;
        this.favorites = favorites;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_favorite_course_list, null);

        LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
                ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        view.setLayoutParams(lp);

        return new ViewHolder(view);

    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
        ViewHolder viewHolder = (ViewHolder) holder;

        FavoriteListMessageLS.FavoriteListEntity.CourseWithOtherInfoEntity course = favorites.get(position).getCourseWithOtherInfo();


        viewHolder.courseName.setText(course.getName());
//        String agencyName="";
//        if (null==course.getAgencyName()&&null!=course.getTeacher())
//            agencyName=course.getTeacher();
//        else
//            agencyName=course.getAgencyName();
      viewHolder.agencyName.setText("(" + course.getAgencyName()+ ")");
       viewHolder.courseType.setText(course.getTypeName());
        viewHolder.realPrice.setText("￥" + course.getCoursePrice());
        viewHolder.actualPrice.setText("￥" + course.getActualPrice());

        viewHolder.address.setText(course.getSchoolAdress());

        String imageUrl = String.format(Config.IMAGE_LOAD_URL_PARAMS, course.getThumbnail());
        ImageUtils.loadImage(context, viewHolder.agencyImage, imageUrl, R.mipmap.def_course_ico);

        //计算距离
        XApplication application = (XApplication) context.getApplicationContext();
        if (null != application &&
                null != course.getLongtitude() && null != course.getLatitude() &&
                null != application.longitude && null != application.latitude) {
            String distance = LngLatUtil.distanceFormatted(course.getLongtitude().doubleValue(), course.getLatitude().doubleValue(),
                    application.longitude, application.latitude);

            if (!StringUtils.isEmpty(distance)) {
                viewHolder.distance.setText(distance);
            }
        }
    }

    @Override
    public int getItemCount() {
        return favorites.size();
    }

    class ViewHolder extends RecyclerView.ViewHolder
            implements View.OnClickListener, View.OnLongClickListener {

        private RelativeLayout detailItem;
        private LinearLayout deleteItem;
        public ImageView agencyImage;
        public TextView courseName;
        public TextView courseType;
        public TextView address;
        public TextView distance;
        public TextView realPrice;
        public TextView actualPrice;
        public TextView agencyName;
        public SwipeLayout root;

        public ViewHolder(View itemView) {
            super(itemView);

            detailItem = (RelativeLayout) itemView.findViewById(R.id.detail_item);
            deleteItem = (LinearLayout) itemView.findViewById(R.id.delete_item);

            detailItem.setOnClickListener(this);
            detailItem.setOnLongClickListener(this);

            agencyImage = (ImageView) itemView.findViewById(R.id.agency_img);
            courseName = (TextView) itemView.findViewById(R.id.course_name);
            courseType = (TextView) itemView.findViewById(R.id.course_type);
            address = (TextView) itemView.findViewById(R.id.address);
            distance = (TextView) itemView.findViewById(R.id.distance);
            realPrice = (TextView) itemView.findViewById(R.id.real_price);
            actualPrice = (TextView) itemView.findViewById(R.id.actual_price);
            agencyName = (TextView) itemView.findViewById(R.id.agency_name);
            root = (SwipeLayout) itemView.findViewById(R.id.root);

            root.addSwipeListener(new SwipeLayout.SwipeListener() {
                @Override
                public void onStartOpen(SwipeLayout swipeLayout) {

                }

                @Override
                public void onOpen(SwipeLayout swipeLayout) {
                    LinearLayout bottomWrap = (LinearLayout) swipeLayout.findViewById(R.id.bottom_wrapper);
                    LinearLayout deleteItem = (LinearLayout) swipeLayout.findViewById(R.id.delete_item);
                    ImageView deleteImage = (ImageView) swipeLayout.findViewById(R.id.delete_image);
                    TextView deleteText = (TextView) swipeLayout.findViewById(R.id.delete_text);

                    View.OnClickListener listener = new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            if (null != onRecyclerViewListener) {
                                onRecyclerViewListener.onDelete(ViewHolder.this.getAdapterPosition());
                            }
                        }
                    };

                    bottomWrap.setOnClickListener(listener);
                    deleteItem.setOnClickListener(listener);
                    deleteImage.setOnClickListener(listener);
                    deleteText.setOnClickListener(listener);
                }

                @Override
                public void onStartClose(SwipeLayout swipeLayout) {

                }

                @Override
                public void onClose(SwipeLayout swipeLayout) {

                }

                @Override
                public void onUpdate(SwipeLayout swipeLayout, int i, int i1) {

                }

                @Override
                public void onHandRelease(SwipeLayout swipeLayout, float v, float v1) {

                }
            });
        }

        @Override
        public void onClick(View v) {
            if (null != onRecyclerViewListener) {
                if (v.getId() == R.id.detail_item) {
                    onRecyclerViewListener.onItemClick(this.getAdapterPosition());
                }
            }
        }

        @Override
        public boolean onLongClick(View v) {
            if (null != onRecyclerViewListener) {
                return onRecyclerViewListener.onItemLongClick(this.getAdapterPosition());
            }

            return false;
        }
    }
}
